import 'dart:io';

import 'package:core_package/core_package.dart';
import 'package:core_package/router/basics/router.dart' as a;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_unionad/core/configuration.dart';
import 'package:flutter_app_unionad/request/promotion_request.dart';
import 'package:flutter_app_unionad/router/routes.dart';
import 'package:provider/provider.dart';
// import 'package:sharesdk_plugin/sharesdk_interface.dart';
import 'main/my_app.dart';
import 'main/providers.dart';
import 'package:flutter_unionad/flutter_unionad.dart' as FlutterUnionad;

main() async {
  final router = a.Router(); // Router();
  Routes.configureRoutes(router);
  Routes.router = router;
  WidgetsFlutterBinding.ensureInitialized(); //必须要添加这个进行初始化 否则下面会错误
  //强制竖屏
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown, DeviceOrientation.portraitUp])
      .then((value) async {
    // 沉浸式状态栏
    if (Platform.isAndroid) {
      SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }

    //http://180.76.108.117:9001
    await UserCacheUil.getInstance(); //缓存数据开启
    // if (UserCacheUil.getBool('isFirstIntall') == true) {
    //   Configuration.loginOut();
    //   UserCacheUil.setBool('isFirstIntall', true);
    // }
    // UserCacheUil.setString(Configuration.openid, '7a31c7fe4eaf43dd823350437eec68f7');
    // UserCacheUil.setInt(Configuration.userId, 939);

    // HttpRequest.init(url: "http://34216723wu.eicp.vip");//开发
    //HttpRequest.init(url: "http://ceshi.51uuid.com"); //测试

    String? agreeProtocol = UserCacheUil.getString('Protocol');
    if ((agreeProtocol != null && agreeProtocol.length != 0)) {
      print('run--------------------------_>1');

      HttpRequest.init(url: "https://www.51uuid.com"); //线上

      // SharesdkPlugin.uploadPrivacyPermissionStatus(1, (bool? success) {
      //   if (success == true) {
      //     print("隐私协议授权提交结果    成功");
      //   } else {
      //     print("隐私协议授权提交结果    失败");
      //   }
      // });
      final f = await FlutterUnionad.FlutterUnionad.register(
        androidAppId: "5059869",
        //穿山甲广告 Android appid 必填
        iosAppId: "5039476",
        //穿山甲广告 ios appid 必填
        useTextureView: true,
        //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView 选填
        appName: "汼汼生活",
        //appname 必填
        allowShowNotify: true,
        //是否允许sdk展示通知栏提示 选填
        allowShowPageWhenScreenLock: true,
        //是否在锁屏场景支持展示广告落地页 选填
        debug: false,
        //测试阶段打开，可以通过日志排查问题，上线时去除该调用 选太难
        supportMultiProcess: true,
        //是否支持多进程，true支持 选填
        // directDownloadNetworkType: [
        //   // FlutterUnionad.FlutterUnionadNetCode.NETWORK_STATE_MOBILE,
        //   // FlutterUnionad.FlutterUnionadNetCode.NETWORK_STATE_2G,
        //   // FlutterUnionad.FlutterUnionadNetCode.NETWORK_STATE_3G,
        //   FlutterUnionad.FlutterUnionadNetCode.NETWORK_STATE_4G,
        //   FlutterUnionad.FlutterUnionadNetCode.NETWORK_STATE_WIFI
        // ],
      );

      try {
        final list = await PromotionRequest.getListByType(type: 1);
        Configuration.promotionList = list;
      } catch (e) {
        print('');
      }
    } else {
      await Future.delayed(Duration(seconds: 1));
      print('run--------------------------_>');
    }
    SizeFill.getInstance();

    runApp(
      MultiProvider(
        providers: providers, //状态管理
        child: MyApp(),
      ),
    );
  });
}
